package com.tepth.remote.ui.recycler;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.joanzapata.iconify.widget.IconTextView;
import com.tepth.remote.ui.R;
import com.tepth.remote.ui.recycler.grid.GridItemBean;
import com.tepth.remote.ui.recycler.grid.GridItemType;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/14
 */
public class MultipleRecyclerAdapter extends
        BaseMultiItemQuickAdapter<MultipleItemEntity, MultipleViewHolder> {
    /**
     * Recycler通用点击回调
     */
    private MultipleRecyclerClickListener mClickListener;
    /**
     * 主页底部菜单的数量
     */
    private static final int GRID_DATA_LENGTH = 4;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    protected MultipleRecyclerAdapter(List<MultipleItemEntity> data, MultipleRecyclerClickListener clickListener) {
        super(data);
        this.mClickListener = clickListener;
        init();
    }

    public static MultipleRecyclerAdapter create(List<MultipleItemEntity> data, MultipleRecyclerClickListener clickListener) {
        return new MultipleRecyclerAdapter(data, clickListener);
    }

    public static MultipleRecyclerAdapter create(BaseDataConverter converter, MultipleRecyclerClickListener mClickListener) {
        return new MultipleRecyclerAdapter(converter.convert(), mClickListener);
    }

    /**
     * 初始化几种Recycler的布局文件
     */
    private void init() {
        addItemType(ItemType.VIEW_PAGER, R.layout.item_multiple_view_pager);
        addItemType(ItemType.PROJECT_PAGER, R.layout.item_multiple_project_view);
        addItemType(ItemType.HISTORY_LIST, R.layout.item_multiple_history_view);
        addItemType(ItemType.FILE_LIST, R.layout.item_multiple_file_view);
        addItemType(ItemType.PICTURE_LIST, R.layout.item_multiple_picture_view);
        addItemType(ItemType.APPLY_HISTORY_LIST, R.layout.item_multiple_apply_history_view);
        //多次执行动画
        isFirstOnly(false);
    }

    @Override
    protected void convert(MultipleViewHolder holder, final MultipleItemEntity entity) {
        switch (holder.getItemViewType()) {
            case ItemType.VIEW_PAGER:
                final ArrayList<GridItemBean> gridData = entity.getField(MultipleFields.VIEW_PAGER_GROUP_LIST);
                final int size = gridData.size();
                if (size < GRID_DATA_LENGTH) {
                    final int s = GRID_DATA_LENGTH - size;
                    for (int i = 0; i < s; i++) {
                        gridData.add(new GridItemBean(GridItemType.DEFAULT, "", ""));
                    }
                }
                final LinearLayoutCompat linearLayoutCompat = holder.getView(R.id.linear_view_pager);
                linearLayoutCompat.addView(getViewPagerLinearLayout(gridData, false));
                linearLayoutCompat.addView(getViewPagerLinearLayout(gridData, true));
                break;
            case ItemType.PROJECT_PAGER:
                final String projectTitle = entity.getField(MultipleFields.TEXT);
                final int projectPosition = entity.getField(MultipleFields.POSITION);
                holder.setText(R.id.text_project_title, projectTitle);
                holder.getView(R.id.icon_project_back).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mClickListener != null) {
                            mClickListener.onclickListener(MultipleFields.BACK, projectPosition);
                        }
                    }
                });
                holder.getView(R.id.icon_project_pull).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mClickListener != null) {
                            final boolean isPull = entity.getField(MultipleFields.IS_PULL);
                            if (isPull) {
                                entity.setField(MultipleFields.IS_PULL, false);
                            } else {
                                entity.setField(MultipleFields.IS_PULL, true);
                            }
                            setData(projectPosition, entity);
                            mClickListener.onclickListener(MultipleFields.PULL, projectPosition);
                        }
                    }
                });
                break;
            case ItemType.HISTORY_LIST:
                final String historyTitle = entity.getField(MultipleFields.TITLE);
                final String historyTime = entity.getField(MultipleFields.TEXT);
                final int id = entity.getField(MultipleFields.ID);
                holder.setText(R.id.text_history_title, historyTitle);
                holder.setText(R.id.text_history_time, historyTime);
                //点击事件
                holder.getView(R.id.item_history_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mClickListener != null) {
                            mClickListener.onclickListener(MultipleFields.HISTORY_BEAN, id);
                        }
                    }
                });
                break;
            case ItemType.FILE_LIST:
                final String fileTitle = entity.getField(MultipleFields.TITLE);
                final String fileTime = entity.getField(MultipleFields.TEXT);
                final String fileSize = entity.getField(MultipleFields.SPAN_SIZE);
                final int fileStatus = entity.getField(MultipleFields.TAG);
                final int fileId = entity.getField(MultipleFields.ID);
                holder.setText(R.id.text_file_title, fileTitle);
                holder.setText(R.id.text_file_time, fileTime + "\b\b" + fileSize);
                holder.setText(R.id.icon_file_operate,
                        fileStatus == ItemType.FILE_COMPLETE ? "{icon-file-open}" :
                                fileStatus == ItemType.FILE_DOWNING ? "{icon-loading}" : "{icon-down}");
                holder.getView(R.id.item_file_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mClickListener != null) {
                            mClickListener.onclickListener(MultipleFields.FILE_BEAN, fileId);
                        }
                    }
                });
                break;
            case ItemType.PICTURE_LIST:
                final String pictureTitle = entity.getField(MultipleFields.TITLE);
                final String pictureName = entity.getField(MultipleFields.NAME);
                final String pictureTime = entity.getField(MultipleFields.TEXT);
                final String pictureId = entity.getField(MultipleFields.ID);
                holder.setText(R.id.text_picture_title, pictureName);
                holder.setText(R.id.text_picture_time, "上传者：" + pictureTitle + "\b\b" + pictureTime.substring(0,
                        pictureTime.indexOf("T")));
                holder.getView(R.id.item_picture_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mClickListener != null) {
                            mClickListener.onclickListener(MultipleFields.PICTURE_BEAN, Integer.parseInt(pictureId));
                        }
                    }
                });
                break;
            case ItemType.APPLY_HISTORY_LIST:
                final String applyHistoryName = entity.getField(MultipleFields.NAME);
                final String applyHistoryTime = entity.getField(MultipleFields.TITLE);
                final String applyHistoryContext = entity.getField(MultipleFields.TEXT);
                final int applyHistoryId = entity.getField(MultipleFields.ID);
                final int applyHistoryTag = entity.getField(MultipleFields.TAG);
                holder.setText(R.id.text_apply_history_name, applyHistoryName);
                holder.setText(R.id.text_apply_history_time, applyHistoryTime);
                holder.setText(R.id.text_apply_history_context, applyHistoryContext);
                holder.setVisible(R.id.icon_apply_history_complete, (applyHistoryTag == 1 || applyHistoryTag == 2));
                if (applyHistoryTag == 1) {
                    holder.setText(R.id.icon_apply_history_complete, "{icon-handle-agree}");
                    holder.setTextColor(R.id.icon_apply_history_complete, ContextCompat.getColor(mContext, R.color.home_color));
                } else if (applyHistoryTag == 2) {
                    holder.setText(R.id.icon_apply_history_complete, "{icon-handle-disagree}");
                    holder.setTextColor(R.id.icon_apply_history_complete, ContextCompat.getColor(mContext, R.color.handle_disagree));
                }
                holder.getView(R.id.item_apply_history_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mClickListener != null) {
                            mClickListener.onclickListener(MultipleFields.APPLY_HISTORY_BEAN, applyHistoryId);
                        }
                    }
                });
            default:
                break;
        }
    }

    /**
     * 添加ViewPager里面的子布局
     *
     * @param gridItemBeans GridItemBean集合
     * @param isBottom      是否是底部布局
     * @return 子布局
     */
    private LinearLayoutCompat getViewPagerLinearLayout(ArrayList<GridItemBean> gridItemBeans, boolean isBottom) {
        final LinearLayoutCompat linearLayoutCompat = new LinearLayoutCompat(mContext);
        final LinearLayoutCompat.LayoutParams linearLayoutParams = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);
        linearLayoutCompat.removeAllViews();
        if (isBottom) {
            linearLayoutParams.setMargins(0, 1, 0, 0);
        }
        linearLayoutCompat.setLayoutParams(linearLayoutParams);
        linearLayoutCompat.addView(getGridLinearLayout(gridItemBeans.get(isBottom ? 2 : 0), false));
        linearLayoutCompat.addView(getGridLinearLayout(gridItemBeans.get(isBottom ? 3 : 1), true));
        return linearLayoutCompat;
    }

    /**
     * 添加Grid里面的子布局
     *
     * @param bean    需要布局的对象
     * @param isRight 布局是否靠右
     * @return 子布局
     */
    private LinearLayoutCompat getGridLinearLayout(final GridItemBean bean, boolean isRight) {
        final LinearLayoutCompat linearLayoutCompat = new LinearLayoutCompat(mContext);
        linearLayoutCompat.setGravity(Gravity.CENTER);
        linearLayoutCompat.setOrientation(LinearLayoutCompat.HORIZONTAL);
        linearLayoutCompat.setBackgroundResource(R.drawable.view_pager_background);
        final LinearLayoutCompat.LayoutParams linearLayoutParams = new LinearLayoutCompat.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
        if (isRight) {
            linearLayoutParams.setMarginStart(1);
        }
        linearLayoutCompat.setLayoutParams(linearLayoutParams);
        final LinearLayoutCompat.LayoutParams textViewLayoutParams = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final IconTextView iconTextView = new IconTextView(mContext);
        iconTextView.setTextSize(35);
        iconTextView.setText(bean.getIcon());
        iconTextView.setTextColor(ContextCompat.getColor(mContext, android.R.color.white));
        iconTextView.setLayoutParams(textViewLayoutParams);
        final AppCompatTextView textView = new AppCompatTextView(mContext);
        textView.setTextSize(20);
        textView.setText(bean.getTitle());
        textView.setTextColor(ContextCompat.getColor(mContext, android.R.color.white));
        textViewLayoutParams.setMarginStart(20);
        textView.setLayoutParams(textViewLayoutParams);
        textView.setPadding(10, 0, 0, 0);
        linearLayoutCompat.addView(iconTextView);
        linearLayoutCompat.addView(textView);
        linearLayoutCompat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.onclickViewPagerListener(bean.getType());
                }
            }
        });
        return linearLayoutCompat;
    }
}
